// pages/classify/child/Select.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputShowed: false,
    show: true,
    price: '0.00',
    num: 0,
    list1: [],
    list: [{
      id: 0,
      title: '轮胎',
      price: '220.9',
      stock: 0,
      number: 0
    }, {
      id: 1,
      title: '方向盘',
      price: '420.9',
      stock: 1,
      number: 0
    }, {
      id: 2,
      title: '方向盘',
      price: '420.9',
      stock: 8,
      number: 0
    }, {
      id: 3,
      title: '方向盘',
      price: '420.9',
      stock: 8,
      number: 0
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  inputfocus() {
    this.setData({
      inputShowed: true
    })
  },
  inputblur() {
    this.setData({
      inputShowed: false
    })
  },
  numberjia(e) {
    let index = e.currentTarget.dataset.index
    let num = this.data.num
    let list = this.data.list;
    let list1 = this.data.list1
    let price = Number(this.data.price);
    if (list[index].stock > 0 && list[index].stock > list[index].number) {
      list1 = []
      num = 0;
      list[index].number++
      price += Number(list[index].price)
      for (let item of list) {
        if (item.number > 0) {
          num++
          item.price = Number(item.price).toFixed(2)
          list1.push(item)
        }
      }
    }
    for(let i=0;i<list1.length;i++) {
      list1[i].price1 = (Number(list1[i].price) * Number(list1[i].number)).toFixed(2);
    }
    this.setData({
      list: list,
      price: price.toFixed(2),
      num: num,
      list1: list1
    })
  },
  numberjian(e) {
    let index = e.currentTarget.dataset.index
    let list = this.data.list 
    let int = list.length;
    let price = Number(this.data.price);
    if (list[index].number > 0) {
      list[index].number--
      price -= Number(list[index].price)
    }
    for (let item of list) {
      if (item.number == 0) {
        int = int-1
      }
    }
    this.setData({
      list: list,
      price: price.toFixed(2),
      num: int
    })
  },
  hide() {
    this.setData({
      show: true
    })
  },
  show() {
    this.setData({
      show: false
    })
  },
  delete(e){
    let list1 = this.data.list1
    const id = e.currentTarget.dataset.id
    let price = this.data.price
    let list = this.data.list
    let num = this.data.num
    let show = this.data.show
    for(let i=0;i<list1.length;i++) {
      if(list1[i].id == id) {
        num--
        price = Number(price).toFixed(2) - (Number(list1[i].price) * Number(list1[i].number)).toFixed(2)
        list1.splice(i,1)
      }
    }
    for(let i=0;i<list.length;i++) {
      if(list[i].id==id) {
        list[i].number = 0
      }
    }
    if(list1.length == 0) show = true
    this.setData({
      list1: list1,
      price: price.toFixed(2),
      list: list,
      num: num,
      show: show
    })
  }
})